package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.SysRoleEntity;
import com.newway.back.entity.SysUserEntity;
import com.newway.back.service.SysRoleService;
import com.newway.common.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/back/sys/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    @GetMapping("/roleList")
    //@PreAuthorize("hasAuthority('USER_READ')")
    public Result getSysRoleList(SysRoleEntity sysRoleEntity){
        Page<SysRoleEntity> page = new Page<>();
        page.setSize(sysRoleEntity.getPageSize());
        page.setCurrent(sysRoleEntity.getPageNum());
        QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysRoleEntity.getName())){
            queryWrapper.like("name",sysRoleEntity.getName());
        }
        Page<SysRoleEntity> result = sysRoleService.page(page, queryWrapper);
        return Result.success(result);
    }

    /**
     * 角色删除
     * @param id
     * @return
     */

    @PostMapping("/roleDel/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result roleDel(@PathVariable("id") Long id){
        if(id==null){
            return Result.fail("唯一标识不能为空");
        }
        boolean b = sysRoleService.removeById(id);
        return Result.success(b);
    }

    @PostMapping("/roleEdit")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    public Result roleDel(@RequestBody SysRoleEntity sysRoleEntity){
        if(sysRoleEntity.getId()==null){
            return Result.fail("唯一标识不能为空");
        }
        boolean b = sysRoleService.updateById(sysRoleEntity);
        return Result.success(b);
    }
}
